<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
		<script type="text/javascript" src="../../../JS/MarketShareFunction/DialogShareFunction.js"></script>
		<script type="text/javascript" src="../../../JS/addRowShareFunction.js" ></script>
	</head>

	<body style="padding:0px;background-color: white">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div>
			<div style="float: right;margin-top: 0px;max-width:60px;" id="notreview"></div>
			<form id="ff" method="post">
				<table id="basicInfo" cellpadding="5" class="displaytable" style="text-align: left;width: 80%;margin: 0px auto;">
					<tr>
						<td width="10%">
							<h id="star">* </h>区域类型</td>
						<td width="25%">
							<input class="easyui-combobox" type="text" width="90%" name="REGION_TYPE" id="input_REGION_TYPE" data-options="required:true" />
							<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="AddRegionTypeName()"></a>
						</td>
						<td width="10%">
							<h id="star">* </h>区域名称</td>
						<td width="25%">
							<input class="easyui-combobox" type="text" width="90%" name="REGION" id="input_REGION" data-options="required:true" />
							<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="AddRegionName()"></a>
						</td>
						<td width="10%">
							<h id="star">* </h>摊位门牌号</td>
						<td width="25%">
							<input class="easyui-combobox" type="text" name="HOUSE_NUMBER" id="input_HOUSE_NUMBER" data-options="required:true" />
						</td>
					</tr>
					<tr>
						<td width="10%">
							<h id="star">* </h>内容</td>
						<td width="25%">
							<input class="easyui-textbox" type="text" name="CONTEXT" id="input_CONTEXT" data-options="required:true" />
						</td>
						<td>
							<h id="star">* </h>维修人员</td>
						<td>
							<input class="easyui-textbox" type="text" name="WORKER" id="input_WORKER" data-options="required:true" />
						</td>
						<td>
							<h id="star">* </h>维修完成时间</td>
						<td>
							<input class="easyui-datebox" type="text" name="FINISH_TIME" id="input_FINISH_TIME" data-options="required:true" />
						</td>
					</tr>
					<tr>
						<td>
							<h id="star">* </h>经办人</td>
						<td>
							<input class="easyui-textbox" type="text" name="OPREATOR" id="input_OPREATOR" data-options="required:true" />
						</td>
						<td>
							<h id="star">* </h>是否报批</td>
						<td>
							<input class="easyui-combobox" type="text" name="SUBMITED" id="input_SUBMITED" />
						</td>
					</tr>
					<tr id="nodisplay" style="display: none">
						<td>ID</td>
						<td>
							<input class="easyui-textbox" type="text" id="input_ID" />
						</td>
						<td>维修金额</td>
						<td>
							<input class="easyui-textbox" type="text" id="input_FEES_SUM" />
						</td>
						<td>维护编号</td>
						<td>
							<input class="easyui-textbox" type="text" id="input_ORDER_NO" data-options="prompt:'由系统自动生成',disabled:true" />
						</td>
					</tr>
				</table>
				<table id="tt" style="margin: 0px auto;width: 100%;">

				</table>
				<div style="padding: 10px 0px;">
					<table>
						<tr>
							<th>人民币金额合计</th>
							<td>
								<input type="text" style="border: none;" id="xx" name="FEES_SUM" />
							</td>
							<th>人民币金额合计(大写)</th>
							<td width="auto">
								<input type="text" style="border: none;" id="dx" />
								<div id="dx"></div>
							</td>
						</tr>
					</table>
				</div>
				<div style="margin-left: 20%;" id="new_footer">

				</div>
			</form>
		</div>
		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 40%;height: 450px;" frameborder="0" scrolling="no"></iframe>
		</div>
	</body>
	<script type="text/javascript">
		var currid = AddFlag; //当前页面的ID
		var id = getID(); //获取上个页面传回的ID
		var currPage = 0; //当前的页数
		var MaxPage = 0;
		var MinPage = 1;
		var currRow;
		var updateData;
		var rowData = new Array();
		var Delrow = 0;
		var isVerify = "0";
		var _detailToolbarData;
		_detailToolbarData = AddDetailToolBar(7);
		$(document).ready(function(){
		if(id != AddFlag && id != undefined) {
			currid = id;
			loadRemote();
		}
		});
		///增加脚注
		getPageMaxAndMin();
		addFooter($("#new_footer"));
		//增加单据的toolbar

		var receipts_type = RECEIPT_FRONT_SYMBOL;
		receipts_type += RECEIPT_NEXT_SYMBOL;
		receipts_type += RECEIPT_ADD_SYMBOL;
		receipts_type += RECEIPT_SAVE_SYMBOL;
		receipts_type += RECEIPT_DELETE_SYMBOL;
		receipts_type += RECEIPT_VERIFY_SYMBOL;
		receipts_type += RECEIPT_UNVERIFY_SYMBOL;
		receipts_type += RECEIPT_SEARCH_SYMBOL;
		receipts_type += RECEIPT_PRINT_SYMBOL;
		receipts_type += RECEIPT_EXPORT_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content); /*在ID=toolbar的标记后面添加content中的内容*/

		IsSubmit();
		GetDetailTable();
		$('#tt').datagrid('hideColumn', 'ID');
		$('#tt').datagrid('hideColumn', 'DEVICE_ID');

		function getPageMaxAndMin() {
			var _url = RemoteLinkAddress + "/market/maintain/getmaxminid.do";
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					if(result != null) {
						MaxPage = result.MAXID;
						MinPage = result.MINID;
						currPage = result.MINID + 1;
					}
				}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		/*区域类型，区域名称，摊位门牌号三级联查*/
		$('#input_REGION_TYPE').combobox({
			valueField: 'NAME',
			textField: 'NAME',
			panelHeight: '150',
			method: 'get',
			mode: 'remote',
			url: RemoteLinkAddress + '/market/regiontype/listall.do',
			onSelect: function(record) {
				RegionData(record.ID);
			}
		});

		function RegionData(type_id) {
			var _url = RemoteLinkAddress + "/market/region/listall.do?TYPE_ID=" + type_id;
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					console.log(result);
					if(result != null) {
						$('#input_REGION').combobox({
							valueField: 'NAME',
							textField: 'NAME',
							panelHeight: '150',
							data: result,
							onSelect: function(record) {
								console.log(record);
								BoostData(record.NAME);
							}
						});
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function BoostData(region_name) {
			var _url = RemoteLinkAddress + "/market/boost/listall.do?REGION_NAME=" + region_name;
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					console.log(result);
					if(result != null) {
						$('#input_HOUSE_NUMBER').combobox({
							valueField: 'ID',
							textField: 'HOUSE_NUMBER',
							panelHeight: '150',
							data: result
						});
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		//是否需要报批
		function IsSubmit() {
			$('#input_SUBMITED').combobox({
				valueField: 'ID',
				textField: 'NAME',
				panelHeight: 'auto',
				data: [{
					'ID': 0,
					'NAME': '否'
				}, {
					'ID': 1,
					'NAME': '是'
				}]
			});
		}
		
		//导出
		function exportExcel(){
			if(currid === AddFlag) {
				$.messager.alert("警告", "该订单为新增订单，无法导出Excel！");
				return;
			}
			$.messager.confirm("提示", "确认导出Excel表格吗？", function(r) {
				if(r) {
					$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
						type: "get",
						data: {
							"ID": currid
						},
						url: RemoteLinkAddress + "/market/maintain/excelorder.do",
						dataType: "json", //服务器返回的数据类型
						success: function(result) {
							window.location.href = result.path;
						}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
						error: function(xhr, status, error) {
							$.messager.alert("错误", "请求失败!");
						}
					});
				}
			});
		}

		///单据相关功能
		function frontpage() { //上一张
			if(currPage < MinPage) {
				$.messager.alert("消息", "没有上张");
				return;
			}
			var _url = RemoteLinkAddress + '/market/maintain/pre.do?ID=' + currPage;
			getRemoteData(_url);
		}

		function nextpage() { //下一张
			if(currPage >= MaxPage) {
				$.messager.alert("消息", "没有下张");
				return;
			}
			var _url = RemoteLinkAddress + '/market/maintain/next.do?ID=' + currPage;
			getRemoteData(_url);
		}

		function newdata() { //新建
			$("#notreview").empty(); //去掉审核或者未审核的图片
			isVerify = 0; //将状态改为未审核
			currid = AddFlag;
			currPage = 0;
			resetData(); //代用该函数是会使默认项为空，即使是有数据，所以重新加载下拉框选项
			IsSubmit();
			$('#tt').datagrid('loadData', {
				total: 0,
				rows: []
			});
		}

		function save() { //保存
			if(isVerify === "1") {
				$.messager.alert("提示", "该单据已审核，不能修改！");
				return;
			}
			var msg = checkDataFormat();
			if(msg != "验证成功") {
				$.messager.alert("警告", msg);
				return;
			}
			var isValid = $("#ff").form('validate');
			if(!isValid) {
				$.messager.alert("警告", "有必填项没有填写");
				return;
			}
			var s=$('#tt').edatagrid('getRows');
			if(s.length==0){
				$.messager.alert("警告", "明细不能为空");
				return;
			}
			if(currid != AddFlag) {
				UpdateData();
			} else {
				SaveData();
			}
		}

		function SaveData() {
			var data = submitFormData();
			var s=$('#tt').edatagrid('getRows');
			data.rows = s;
			submitData(data);
		}

		function UpdateData() {
			var data = submitFormData();
			for(s in data)
			{
				updateData[s]=data[s];
			}
			submitData(updateData);
		}

		function submitData(_data) {
			var _url = "";
			if(currid == AddFlag) {
				_url = RemoteLinkAddress + "/market/maintain/save.do";
			} else {
				_url = RemoteLinkAddress + "/market/maintain/update.do";
			}
			var _senddata = JSON.stringify(_data);
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + _senddata,
				type: "POST",
				success: function(result) {
					if(currid == AddFlag) {
						var revData = result.msg.split(";");
						$("#input_ORDER_NO").textbox("setValue", revData[2]);
						$("#input_ID").textbox("setValue", revData[1]);
						currid=revData[1];
						currPage=revData[1];
						$(document).ready(function(){
							loadRemote();
						});
						$.messager.alert("提示", "新建保存成功");
						return;
					}
					$.messager.alert("提示", "修改保存成功");
				},
				error: function(result) {
					$.messager.alert("错误", "保存不成功");
				}
			});
		}

		function deletedata() { //删除
			if(isVerify === "1") {
				$.messager.alert("提示", "该单据已审核，不能删除！");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("提示", "该页面为新增页面，不能进行删除操作！");
				return;
			}
			$.messager.confirm('Confirm', '是否删除', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/market/maintain/delete.do", {
							ID:currid
						},
						function(result) {
							if(result.msg == "success") {
								$('#tt').datagrid('reload'); // reload the user data
								$.messager.alert({ // show success message
									title: '成功',
									msg: '删除成功'
								});
								nextpage();
							} else {
								$.messager.alert({ // show error message
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}

		function searchmore() { //查询
			AddTabIn("设备维护查询", parent.$('#tabs'), "UI/Market/EquipmentRepairManagement/EquipmentRepairManagementSearch.html");
		}

		function verifydata() { //审核单据
			if(currid == AddFlag) {
				$.messager.alert("提示", "该页面为新增页面，不能进行审核操作！");
				return;
			}
			if(isVerify == 1) {
				$.messager.alert("提示", "该订单已通过审核，无须再审！");
				return;
			}
			var _url = RemoteLinkAddress + "/market/maintain/verify.do";
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: {
					ID: currid
				},
				type: "POST",
				success: function(result) {
					$.messager.alert("成功", "审核成功");
					isVerify = "1";
					verifyStatus(isVerify, true);
				},
				error: function(result) {
					$.messager.alert("失败", "审核失败");
				}
			});
		}

		function unverifydata() { //弃审
			if(currid == AddFlag) {
				$.messager.alert("提示", "该页面为新增页面，不能进行弃审操作！");
				return;
			}
			if(isVerify == "0") {
				$.messager.alert("警告", "已经弃审无须再弃审");
				return;
			}
			$.messager.confirm("警告", "你确认要弃审该条单据吗", function(r) {
				if(r) {
					var _url = RemoteLinkAddress + "/market/maintain/unverify.do";
					$.ajax({
						dataType: 'JSON',
						url: _url,
						data: {
							ID: currid
						},
						type: "POST",
						success: function(result) {
							$.messager.alert("成功", "弃审成功");
							isVerify = "0";
							verifyStatus(isVerify, true);
						},
						error: function(result) {
							$.messager.alert("失败", "弃审失败");
						}
					});
				}
			});
		}

		function print() { //打印
			if(isVerify == "0") {
				$.messager.alert("警告", "单据未审核，不能打印！");
				return;
			}
			var pLocation = "";
			if(currid == AddFlag) {
				$.messager.alert('警示', "当前页面为新增页面，不可打印！");
				return;
			}
			pLocation = "EquipmentRepairManagementPrint.html?id=" + currid;
			window.open(pLocation);
		}

		function loadRemote() {
			var _url = RemoteLinkAddress + '/market/maintain/findbyid.do?ID=' + currid;
			getRemoteData(_url);
		}

		function getRemoteData(_url) {
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result == null) {
						$.messager.alert("消息", "没有上张");
						return;
					}
					if(result != null) {
						isVerify = result.ISVERIFY;
						verifyStatus(isVerify, true);
						getFormData(result);
						updateData=deepCopy(result);
						console.log(updateData);
						currPage = result.ID;
						currid = result.ID;
						var s = result.rows;
						var _index = 0;
						for(var i = 0; i < s.length; i++) {
							s[_index].NAME = s[i].DEVICE_NAME;
							_index++;
						}
						$('#tt').datagrid('loadData', s);
						CalSum();
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function GetDetailTable() { //构造datagrid中的数据
			$('#tt').edatagrid({
				height: 300,
				toolbar: _detailToolbarData,
				collapsible: 'true', //是否折叠：是
				singleSelect: 'true', //是否单选：是
				rownumbers: 'true',
				fitColumns: 'true', //自动调整列：是
				showFooter: 'true',
				autoSave: 'true',
				columns: [
					[{
						field: 'ID',
						title: 'ID',
						width: '0%',
						align: 'center',
					}, {
						field: 'DEVICE_ID',
						title: '维护设备ID',
						width: '0%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "DEVICE_ID",
								textField: "DEVICE_ID",
								editable: false
							}
						}
					}, {
						field: 'NAME',
						title: '设备名称',
						width: '16%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: 'NAME',
								textField: 'NAME',
								mote: 'remote',
								method: 'get',
								editable: true,
								url:RemoteLinkAddress + "/market/device/listall.do",
								onChange: function(newValue, oldValue) {
									if(newValue != "" && newValue != undefined) {
										var url = RemoteLinkAddress + "/market/device/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
										$(this).combobox("reload", url);
									}
								},
								onSelect: function(record) {
									var rowIndex = $("#tt").datagrid('getRowIndex', currRow);

									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'DEVICE_ID'
									}).target;
									target.textbox('clear');
									if(record.ID != undefined) {
										target.textbox('setValue', record.ID);
									}

									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'DEVICE_TYPE'
									}).target;
									target.textbox('clear');
									if(record.DEVICE_TYPE != undefined) {
										target.textbox('setValue', record.DEVICE_TYPE);
									}

									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'DEVICE_RP'
									}).target;
									target.textbox('clear');
									if(record.DEVICE_RP != undefined) {
										target.textbox('setValue', record.DEVICE_RP);
									}

									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'PRODUCTION_TIME'
									}).target;
									target.textbox('clear');
									if(record.PRODUCTION_TIME != undefined) {
										target.textbox('setValue', record.PRODUCTION_TIME);
									}

									var target = $('#tt').datagrid('getEditor', {
										'index': rowIndex,
										'field': 'SERVICELIFE'
									}).target;
									target.textbox('clear');
									if(record.SERVICELIFE != undefined) {
										target.textbox('setValue', record.SERVICELIFE);
									}
								}
							}
						},
					}, {
						field: 'DEVICE_TYPE',
						title: '设备类型',
						width: '10%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "DEVICE_TYPE",
								textField: "DEVICE_TYPE",
								editable: false
							}
						}
					}, {
						field: 'DEVICE_RP',
						title: '设备负责人',
						width: '15%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "DEVICE_RP",
								textField: "DEVICE_RP",
								editable: false
							}
						}
					}, {
						field: 'PRODUCTION_TIME',
						title: '生产日期',
						width: '17%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "PRODUCTION_TIME",
								textField: "PRODUCTION_TIME",
								editable: false
							}
						}
					}, {
						field: 'SERVICELIFE',
						title: '使用年限',
						width: '15%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "SERVICELIFE",
								textField: "SERVICELIFE",
								editable: false,
								precision: 1
							}
						},
						formatter: function(value, row, index) {
							if(value) {
								return value + '年';
							}
						}
					}, {
						field: 'FEES',
						title: '维修金额',
						width: '16%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "FEES",
								textField: "FEES",
								editable: true
							}
						},
						formatter: function(value, row, index) {
							if(value) {
								return '￥' + value + '元';
							}
						}
					}]
				],
				onEndEdit: function(index, row, changes) {
					CalSum();
					if(currid != AddFlag) {
						if(row.isNewRecord == false || row.isNewRecord == undefined) //即修改的情况
						{
							for(var i = 0; i < updateData.rows.length; i++) {
								if(updateData.rows[i].ID == row.ID) {
									var obj = updateData.rows[i];
									for(var s in obj) {
										if(obj[s] != row[s]) {
											obj[s] = row[s];
										}
									}
								}
							}
						} else {
							var len = updateData.rows.length;
							updateData.rows[len] = row;
							updateData.rows[len].ORDER_ID = currid;
						}
					}
				},
				onDblClickRow: function(index, row) {
					currRow = row;
				},
				onAdd: function(index, row) {
					currRow = row;
				},
				onDestroy:function(index,row) {
					CalSum();
					if(currid != AddFlag) {
						for(var i = 0; i < updateData.rows.length; i++) {
							if(updateData.rows[i].ID == row.ID) {
								updateData.rows[i].ORDER_ID = ""; //直接替换
							}
						}
					}
				}
			});
		}

		function CalSum() {
			var sum = Number(0);
			var dataGridRows = $("#tt").datagrid("getRows");
			for(var i = 0; i < dataGridRows.length; i++) {
				if(dataGridRows[i].NAME != undefined) {
					var money = Number(dataGridRows[i].FEES);
					sum = sum + money;
				}
			}
			$("#xx").val(sum);
			$('#input_FEES_SUM').textbox('setValue', sum);
			var _sum = changeMoneyToChinese(sum);
			$("#dx").val(_sum);
		}

		function AddRegionTypeName() {
			var pLocation = "UI/Market/DictData/DictDataAdd.html?id=regiontype";
			$("#ifrmEdit").attr("src", pLocation);
			constructcomboDialog($("#dlg"), "添加基础信息", $("#input_REGION_TYPE"));
			AddDialog.dialog('open');
		}

		function AddRegionName() {
			var pLocation = "UI/Market/DictData/DictDataAdd.html?id=region";
			$("#ifrmEdit").attr("src", pLocation);
			constructcomboDialog($("#dlg"), "添加基础信息", $("#input_REGION"));
			AddDialog.dialog('open');
		}
	</script>

</html>